package com.xcc.opendd;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class TimeAdapter extends RecyclerView.Adapter<TimeAdapter.ViewHolder> {
    private Context context;
    private List<TimeMode> modeList;
    private LayoutInflater inflater;
    private OnAdapterClickListener onAdapterClickListener;

    public TimeAdapter(Context context, List<TimeMode> modeList) {
        this.context = context;
        this.modeList = modeList;
        inflater = LayoutInflater.from(context);
    }

    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.item_time, viewGroup, false);
        return new ViewHolder(view);
    }

    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        TimeMode mode = modeList.get(i);
        viewHolder.setMode(mode);
    }

    public int getItemCount() {
        return modeList.size();
    }

    public TimeAdapter setOnAdapterClickListener(OnAdapterClickListener onAdapterClickListener) {
        this.onAdapterClickListener = onAdapterClickListener;
        return this;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textTime = itemView.findViewById(R.id.textTime);
            itemView.findViewById(R.id.imgDelect).setOnClickListener(this);
        }

        private TimeMode mode;
        private TextView textTime;

        public void setMode(TimeMode mode) {
            this.mode = mode;
            textTime.setText(mode.getTimeStr());
        }

        public void onClick(View v) {
            if (onAdapterClickListener == null) return;
            onAdapterClickListener.onClick(mode);
        }
    }

    public interface OnAdapterClickListener {
        public void onClick(TimeMode mode);
    }
}
